package com.klondike.common;

/**
 * The object reference which hold the other object as referent. This class is a
 * mutable implementation which allows you to change the referent at any time.
 * Also the equality and hashCode of instances of this call depends of the
 * current referent.
 * 
 * @author Munish Gogna
 * 
 * @param <T>
 *            type of referent
 */
public class ObjectReference<T> {

    private T referent;

    public ObjectReference(T referent) {
        this.referent = referent;
    }

    public ObjectReference() {
    }

    public void set(T referent) {
        this.referent = referent;
    }

    public T get() {
        return referent;
    }

    @Override
    public int hashCode() {
        return referent == null ? 0 : referent.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        boolean result = (obj == this);
        if (!result && (obj instanceof ObjectReference<?>)) {
            ObjectReference<?> o = (ObjectReference<?>) obj;
            result = Utils.equals(referent, o.referent);
        }

        return result;
    }

    @Override
    public String toString() {
        return String.valueOf(referent);
    }
}
